<template>
	<view class="welcome_box">
		<Tips></Tips>
		<view class="header">
			<view class="info">
				<view class="tip">
					{{isShare ? '能帮我助一下力吗' : (helpState ? '助力成功' : '助力中...')}}
				</view>
			</view>
			<view class="illustration">
				<image
					style="width: 350rpx; height: 350rpx;" 
					mode="aspectFit"
					:src="joinSystemPath('help.png')"/>
			</view>
		</view>
		<view class="content">
			<view class="submit" v-if="isShare">
				<button class="invite_help" open-type="share">分享助力链接</button>
			</view>
			<view class="submit" v-else>
				<MyButton
					:bgc="primary_hot_color"
					:clickFun="submitHelpJoinFun"
					>助力</MyButton>
			</view>
		</view>
		<view style="position: absolute;bottom: 100rpx;">
			<TipInfo :color="shallow_text_color" :id="ruleId"></TipInfo>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,onShareAppMessage,onShow
	} from '@dcloudio/uni-app'
	import {ref, watch} from 'vue'
	
	import MyInput from '@/components/MyInput/MyInput.vue'
	import MyButton from '@/components/MyButton/MyButton.vue'
	import Tips from '@/components/Tips/Tips.vue'
	import TipInfo from '@/components/TipInfo/TipInfo.vue'
	
	import {
		joinSystemPath,
		joinDormitoryCover
	} from '@/utils/imgJoinName.js'
	
	import {dynamicStyleStore} from '@/stores/dynamicStyle.js'
	import {userStore} from '@/stores/user'
	
	import {DormitoryModel} from '@/service/model/dormitory.js'
	
	import {
		verifyIsLogin,
		verifyFormData
		} from '@/hooks/verify.js'
	
	const user_store=userStore()
	
	const dynamicStyle_store = dynamicStyleStore()
	
	const dormitoryModel = new DormitoryModel()
	
	const {
		primary_hot_color,
		primary_text_color,
		shallow_text_color
	} = dynamicStyle_store.dormitoryStyle
	
	const dormitoryAllId=uni.getStorageSync('homeData')
	const type=ref('')
	const isShare=ref(false)
	const helpRelationId=ref('')
	const helpDutyId = ref('')
	const ruleId=ref('')
	const helpState = ref(false)
	
	//提交助力
	const submitHelpJoinFun=async()=>{
		verifyIsLogin()
		let data,success;
		switch(type.value){
			case "join_bgc_help":
				const {data:bgcData,success:bgcSuccess} = await dormitoryModel.setJoinHelp({relation_id:helpRelationId.value})
				data=bgcData
				success=bgcSuccess
				break;
			case "duty_clock_fill_help":
				const {data:clockData,success:clockSuccess} = await dormitoryModel.postSetFillDutylClockHelp({duty_id:helpDutyId.value})
				data=clockData
				success=clockSuccess
				break
			default:
				uni.$emit('tips','error','页面错误')
		}
		
		if(success){
			uni.$emit('tips',data,'success')
			helpState.value = true
			setTimeout(()=>{
				uni.reLaunch({
					url:`/pages/Dormitory/Dormitory`
				})
			},2000)
		}
	}
	
	onShareAppMessage((res)=>{
		let path;
		switch(type.value){
			case "join_bgc_help":
				path = `/pages/Dormitory/InviteHelp/InviteHelp?relation_id=${helpRelationId.value}&type=${type.value}`
				break;
			case "duty_clock_fill_help":
				path = `/pages/Dormitory/InviteHelp/InviteHelp?duty_id=${helpDutyId.value}&type=${type.value}`
				break
			default:
				uni.$emit('tips','error','页面错误')
		}
		return {
			  title: `帮我助一下力吧，感谢感谢`,
			  path,
			  mpPath:path,
			  imageUrl:joinSystemPath('help.png')
			}
	})
	
	onLoad((opt)=>{
		const {share,type:_type} = opt
		type.value = _type
		let title;
		switch(_type){
			case "join_bgc_help":
				const {relation_id} = opt
				if(relation_id){
					helpRelationId.value = relation_id
					ruleId.value = 3
					if(share==='true'){
						title="邀请好友助力得次数"
						isShare.value=true
					}else{
						title="帮ta助力拿次数"
					}
				}else{
					uni.$emit('tips','info','这个不是邀请链接')
				}
				break;
			case "duty_clock_fill_help":
				const {duty_id} = opt
				if(duty_id){
					helpDutyId.value = duty_id
					ruleId.value = 4
					if(share==='true'){
						isShare.value=true
						title="邀请好友助力得补签卡"
					}else{
						title="帮ta助力拿补签卡"
					}
				}else{
					uni.$emit('tips','info','这个不是邀请链接')
				}
				break
			default:
				uni.$emit('tips','error','页面错误')
		}
		uni.setNavigationBarTitle({
			title
		})
	})
</script>

<style lang="less">
	.welcome_box{
		height: calc(100vh);
		width: 100%;
		.header{
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 0 20rpx;
			.info{
				font-size: 40rpx;
				font-weight: 800;
				color: v-bind(primary_hot_color);
			}
		}
		.content{
			width: 100%;
			.form{
				margin-bottom: 100rpx;
			}
			.submit{
				padding: 0 60rpx;
			}
			.invite_help{
				background-color: v-bind(primary_hot_color);
				color: #FFFFFF;
			}
		}
	}
</style>
